package 栈;

import java.util.Stack;

public class Solution_test {
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for(int i=0;i< tokens.length;i++){
            String str = tokens[i];
            if(!isOperation(str)){
                //不是符号
                int val = Integer.valueOf(str);
                stack.push(val);
            }else{
                //是符号,进行运算
                int n2 = stack.pop();
                int n1 = stack.pop();

                switch (str){
                    case "+":
                        stack.push(n1+n2);
                        break;
                    case "-":
                        stack.push(n1-n2);
                        break;
                    case "*":
                        stack.push(n1*n2);
                        break;
                    case "/":
                        stack.push(n1/n2);
                        break;
                }
            }
        }
        return stack.pop();
    }

    //判断是否是运算符
    private boolean isOperation(String str){
        if(str.equals("+")||str.equals("-")||str.equals("*")||str.equals("/")){
            return true;
        }
        return false;
    }
}
